
package com.taiyuan.zfmvp.controller;


import cn.hutool.core.date.BetweenFormater;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.taiyuan.zfmvp.sharding.model.TestCode;
import com.taiyuan.zfmvp.utils.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


@Controller
@RequestMapping("/test")
public class KafkaController {




    public static void main(String[] args) {

        /*String timestr = "2023-7-3 14:06:54";
        Timestamp timestamp = Timestamp.valueOf(timestr);
        long time2 = timestamp.getTime();
        Date startDate = new Date(time2);
        //相差小时数
        long hour=DateUtil.between(startDate,new Date(),DateUnit.HOUR);
        //相差分钟数
        long minute=DateUtil.between(startDate,new Date(),DateUnit.MINUTE);
        long between = DateUtil.between(startDate, new Date(), DateUnit.MS);
        String formatBetween = DateUtil.formatBetween(between, BetweenFormater.Level.MINUTE);
        //System.out.println(formatBetween);

        Map<String,String> params = new HashMap<>();
        params.put("03","办税员");
        InvoiceTaxersVo invoiceTaxersVo = new InvoiceTaxersVo();
        invoiceTaxersVo.setPassword("111");
        invoiceTaxersVo.setUserName("18632225522");
        invoiceTaxersVo.setAuthName(params);
        System.out.println(JSON.toJSONString(invoiceTaxersVo));

        String str = "{\"msg\":\"成功\",\"code\":\"0000\",\"response\":\"{\\\"data\\\":\\\"\\\",\\\"step_code\\\":\\\"3\\\",\\\"sms_waittime\\\":\\\"\\\"}\"}";
        JSONObject jsonObject = JSON.parseObject(str);
        String resultCode = (String) jsonObject.get("code");

        JSONObject response = jsonObject.getJSONObject("response");
        String step_code = response.getString("step_code");
        System.out.println(resultCode);
        System.out.println(step_code);*/

       /* String content = "qrcode_id=6Na0haNoyHvUZ0nDeg+NTji20fYQ0FMoqwoJ4MADcrQFAdAvmDp7i7Yobk7zzkNM&areaPrefix=1400&interfaceCode=0004";
        String path = "C:\\Users\\86186\\Desktop\\11";
        String filename = "aa";
        try {
            buildQuickMark(content, path, filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("生成的二维码路径在这里" + path);*/
        //String timestr = "2023-07-09 23:06:47";
        /*Timestamp timestamp = Timestamp.valueOf(timestr);
        long startTime = timestamp.getTime();
        long endTime = new Date().getTime();
        long timeDifference = endTime-startTime;
        long hour = timeDifference/1000/60/60;	//计算时
        if(hour>22){
            System.out.println(hour);
        }else{
            System.out.println("还早");
        }*/

        //数据全
        //String result = "{\"code\": \"200\",\"msg\": \"\",\"result\": [{\"idno\": \"130824197111168013\",\"visitType\": \"挂号\",\"visitTime\": \"2023/7/10 17:03:38\"\t},\t{\"idno\": \"130824197111168013\",\"visitType\": \"收费\",\"visitTime\": \"2023/7/10 17:03:37\"\t},\t{\"idno\": \"130824197111168013\",\"visitType\": \"入院\",\"visitTime\": \"2022/8/1 9:09:13\"\t},\t{\"idno\": \"130824197111168013\",\"visitType\": \"入院\",\"visitTime\": \"2023/6/1 10:34:17\"\t},\t{\"idno\": \"130824197111168013\",\"visitType\": \"出院\",\"visitTime\": \"2022/8/8 9:00:37\"\t},\t{\"idno\": \"130824197111168013\",\"visitType\": \"出院\",\"visitTime\": \"2023/6/14 10:13:21\"\t}]  }";
        //入院时间和出院时间比较
        /*String result = "{\"code\": \"200\",\"msg\": \"\",\"result\": [{\"idno\": \"130824197111168013\",\"visitType\": \"挂号\",\"visitTime\": \"2023/7/10 17:03:38\"},{\"idno\": \"130824197111168013\",\"visitType\": \"收费\",\"visitTime\": \"2023/7/10 17:03:37\"},{\"idno\": \"130824197111168013\",\"visitType\": \"入院\",\"visitTime\": \"2022/8/1 9:09:13\"},{\"idno\": \"130824197111168013\",\"visitType\": \"入院\",\"visitTime\": \"2023/6/1 10:34:17\"}]  }";
        JSONObject resultJson = JSONObject.parseObject(result);
        List<HosInfoVo> responseParams = resultJson.getJSONArray("result").toJavaList(HosInfoVo.class);
        //取出能开的数据
        List<HosInfoVo> returnVos =  getHosDatas(responseParams);
        //分析并对出入院信息进行取值
        List<HosInfoVo> resultVos =  optHosDatas(returnVos);*/



        /*for (int i = 0; i < resultVos.size(); i++) {
            System.out.println(resultVos.get(i).getVisitType());
            System.out.println(resultVos.get(i).getVisitTime());
            System.out.println(resultVos.get(i).getIdno());
        }*/

        /*String strtime = "140107199306165214";
        String newstr = strtime.replace("/","-");
        //System.out.println(newstr);
        Map<String, Object> inParam = new HashMap<>();
        inParam.put("idno", strtime);
        System.out.println(JSON.toJSONString(inParam));

        String str = "{\"code\":0,\"msg\":\"未查询到用户\",\"result\":null}";
        JSONObject resul = JSONObject.parseObject(str);
        Integer code = (Integer) resul.get("code");

        if (!code.equals(0)) {
            System.out.println("异常");
        }else {
            System.out.println("成功");
        }*/

        String zipFilePath = "C:\\Users\\86186\\Desktop\\物贸物流\\14010701.zip";
        String destDirectory = "C:\\Users\\86186\\Desktop\\物贸物流\\zipfile";

        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdir();
        }

        try {
            FileInputStream fis = new FileInputStream(zipFilePath);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
                File newFile = new File(destDirectory, ze.getName());
                FileOutputStream fos = new FileOutputStream(newFile);
                byte[] buffer = new byte[1024];
                int len;

                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                ze = zis.getNextEntry();
            }

            zis.closeEntry();
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File[] files = destDir.listFiles();
        for (File file : files) {
            if (file.isFile()) {
                System.out.println(file.getName());
            }
        }

    }

    public static List<HosInfoVo> optHosDatas(List<HosInfoVo> returnVos){
        String inTime = "";
        String outTime = "";
        for (int i = 0; i < returnVos.size(); i++) {
            //拿到入院和出院日期
            if("入院".equals(returnVos.get(i).getVisitType())){
                inTime = returnVos.get(i).getVisitTime();
            }
            if("出院".equals(returnVos.get(i).getVisitType())){
                outTime = returnVos.get(i).getVisitTime();
            }
        }
        //三种情况的判断
        //1.入院日期为空，那出院日期肯定也为空， 入院，出院日期都塞空字符串
        //2.出院日期为空，说明只有入院日期， 出院日期塞空字符串
        //3.出入院日期都不为空，那就比较，如果入院小于出院，不做操作，
        //如果入院时间大于出院时间，说明出院日期为上次的数据，和入院日期对不上，就诊人还没出院，出院日期塞空字符串
        if(StringUtils.isBlank(inTime)){
            //入院日期为空
            for (int i = 0; i < returnVos.size(); i++) {
                if("入院".equals(returnVos.get(i).getVisitType())){
                    returnVos.get(i).setVisitTime("");
                }
                if("出院".equals(returnVos.get(i).getVisitType())){
                    returnVos.get(i).setVisitTime("");
                }
            }
        }else if(StringUtils.isBlank(outTime)){
            //出院日期为空
            for (int i = 0; i < returnVos.size(); i++) {
                if("出院".equals(returnVos.get(i).getVisitType())){
                    returnVos.get(i).setVisitTime("");
                }
            }
        }else if(StringUtils.isNotBlank(inTime) && StringUtils.isNotBlank(outTime)){
            //比较日期
            if(inTime.compareTo(outTime) > 0) {
                //入院时间>出院时间，出院时间塞空值
                for (int i = 0; i < returnVos.size(); i++) {
                    if("出院".equals(returnVos.get(i).getVisitType())){
                        returnVos.get(i).setVisitTime("");
                    }
                }
            }else{
                System.out.println("入院时间小于出院时间，不做操作");
            }
        }

        return returnVos;
    }


    public static List<HosInfoVo> getHosDatas(List<HosInfoVo> responseParams ){
        List<HosInfoVo> returnVos = new ArrayList<>();
        List<HosInfoVo> returnVoSort = new ArrayList<>();
        for (HosType hosType : HosType.values()) {
            for (int i = 0; i < responseParams.size(); i++) {
                if(hosType.toString().equals(responseParams.get(i).getVisitType())){
                    HosInfoVo returnVo = new HosInfoVo();
                    returnVo.setIdno(responseParams.get(i).getIdno());
                    returnVo.setVisitTime(responseParams.get(i).getVisitTime());
                    returnVo.setVisitType(responseParams.get(i).getVisitType());
                    returnVoSort.add(returnVo);
                }
            }

            Collections.sort(returnVoSort);
            if(returnVoSort.size()>0){
                returnVos.add(returnVoSort.get(0));
            }
            returnVoSort.clear();
        }
        return returnVos;

        /*for (int i = 0; i < responseParams.size(); i++) {
            if("挂号".equals(responseParams.get(i).getVisitType())){
                HosInfoVo returnVo = new HosInfoVo();
                returnVo.setIdno(responseParams.get(i).getIdno());
                returnVo.setVisitTime(responseParams.get(i).getVisitTime());
                returnVo.setVisitType(responseParams.get(i).getVisitType());
                returnVoSort.add(returnVo);
            }
        }

        Collections.sort(returnVoSort);
        if(returnVoSort.size()>0){
            returnVos.add(returnVoSort.get(0));
        }
        returnVoSort.clear();

        for (int i = 0; i < responseParams.size(); i++) {
            if("收费".equals(responseParams.get(i).getVisitType())){
                HosInfoVo returnVo = new HosInfoVo();
                returnVo.setVisitType(responseParams.get(i).getVisitType());
                returnVo.setVisitTime(responseParams.get(i).getVisitTime());
                returnVo.setIdno(responseParams.get(i).getIdno());
                returnVoSort.add(returnVo);
            }
        }

        Collections.sort(returnVoSort);
        if(returnVoSort.size()>0){
            returnVos.add(returnVoSort.get(0));
        }
        returnVoSort.clear();

        for (int i = 0; i < responseParams.size(); i++) {
            if("入院".equals(responseParams.get(i).getVisitType())){
                HosInfoVo returnVo = new HosInfoVo();
                returnVo.setIdno(responseParams.get(i).getIdno());
                returnVo.setVisitTime(responseParams.get(i).getVisitTime());
                returnVo.setVisitType(responseParams.get(i).getVisitType());
                returnVoSort.add(returnVo);
            }
        }
        Collections.sort(returnVoSort);
        if(returnVoSort.size()>0){
            returnVos.add(returnVoSort.get(0));
        }
        returnVoSort.clear();

        for (int i = 0; i < responseParams.size(); i++) {
            if("出院".equals(responseParams.get(i).getVisitType())){
                HosInfoVo returnVo = new HosInfoVo();
                returnVo.setIdno(responseParams.get(i).getIdno());
                returnVo.setVisitTime(responseParams.get(i).getVisitTime());
                returnVo.setVisitType(responseParams.get(i).getVisitType());
                returnVoSort.add(returnVo);
            }
        }
        Collections.sort(returnVoSort);
        if(returnVoSort.size()>0){
            returnVos.add(returnVoSort.get(0));
        }
        returnVoSort.clear();

        return returnVos;*/
    }

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    /**
     * 根据字符串生成对应的二维码图片
     * 大小:200*200
     * content：要转换的内容
     * path：生成的二维码图片的绝对路径
     * filename: 生成的文件名
     */
    public static String buildQuickMark(String content, String path, String filename) throws Exception {
        String flieUrl="null";
        try {
            BitMatrix byteMatrix = new MultiFormatWriter().encode(new String(content.getBytes(), "iso-8859-1"),
                    BarcodeFormat.QR_CODE, 200, 200);
            String format = "png";
            File file = new File(path + "\\" + filename + "." + format);
            BufferedImage image = toBufferedImage(byteMatrix);
            if (!ImageIO.write(image, format, file)) {
                throw new IOException("Could not write an image of format " + format + " to " + file);
            }
            flieUrl = file.getPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flieUrl;
    }

    private static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }


    //手机号码脱敏(保留第4到第7位)
    private static String desensitizedPhoneNum(String phoneNum) {
        if(StringUtils.isBlank(phoneNum) || "null".equals(phoneNum)){
            return "";
        }
        if(Pattern.matches("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$",phoneNum)){
            return phoneNum.substring(0, 3)+phoneNum.substring(3,7).replaceAll(".{1}","\\*")+
                    phoneNum.substring(7, phoneNum.length());
        }
        return phoneNum;
    }


    /**
     * @Author zhoufang@sxjtjt.com.cn
     * @Description
     * @Date 2023/7/6
     * @Param
     * @Return
     */
    @PostMapping("/fasongyanzm")
    @ResponseBody
    public String fasongyanzm(@RequestBody Map<String, Object> inMap) {


        return getLoginInfo();
    }

    /**
     * @Author zhoufang@sxjtjt.com.cn
     * @Description
     * @Date 2023/7/6
     * @Param
     * @Return
     */
    @PostMapping("/renzheng")
    @ResponseBody
    public String renzheng(@RequestBody Map<String, Object> inMap) {
        String code = String.valueOf(inMap.get("code"));
        return getLoginIn(code);
    }

    /**
     * @Author zhoufang@sxjtjt.com.cn
     * @Description
     * @Date 2023/7/6
     * @Param
     * @Return
     */
    @PostMapping("/kaipiao")
    @ResponseBody
    public String kaipiao(@RequestBody Map<String, Object> inMap) {
        return kaipiao();
    }

    public static  String kaipiao(){
        String access_token = getToken();
        System.out.println("access_token:"+access_token);
        //调用发票开具接口
        String params = "{\"fptxx\":{\"djrq\":\"2021-04-27 12:30:30\",\"xsdh\":\"2023051000000001\",\"fjh\":\"0-1\",\"fplx\":\"82\",\"gfmc\":\"太原有限公司\",\"gfnsrsbh\":\"91141088MA7Y38PK1W\",\"gfdzdh\":\"\",\"gfyhjzh\":\"\",\"gfsj\":\"15888668866\",\"gfyx\":\"\",\"bz\":\"\",\"kpy\":\"冯琦\",\"sky\":\"\",\"fhr\":\"\",\"xfmc\":\"\",\"xfnsrsbh\":\"91140000MA0L9DU60L\",\"xfdzdh\":\"\",\"xfyhjzh\":\"\",\"hsbz\":\"1\",\"wctz\":\"0\",\"chyy\":\"\",\"fpxz\":\"0\",\"yfpdm\":\"\",\"yfphm\":\"\",\"sfsgp\":\"\",\"sfcpy\":\"\",\"hzxxbbh\":\"\"},\"xmxxs\":[{\"spfldm\":\"\",\"splwmc\":\"商品名称2\",\"ggxh\":\"\",\"dw\":\"套\",\"count\":\"1\",\"price\":\"0.01\",\"je\":\"0.01\",\"sl\":\"0.06\",\"se\":\"0.01\",\"ssflbm\":\"3060110010000000000\",\"bmbbh\":\"\",\"lslbs\":\"\",\"yhzcbs\":\"0\",\"yhzcsm\":\"\",\"fphxz\":\"0\",\"kce\":\"\"}]}";
        String executeParams = "{'org_code':'600001','sdyh':'18335109596','nsrsbh':'91140000MA0L9DU60L','serve_code':'yspt_sdp_kp','access_token':'" + access_token + "'," + "'data':'" + getAesData(JSON.toJSON(params).toString(), "gyZ9qah3QzPrmQZa") + "'}";
        String url = "http://172.16.95.5:38080/yspt/sdpAccept";
        System.out.println("发票请求地址："+ url);
        System.out.println("发票请求参数："+ executeParams);
        String result = null;
        try {
            result = sendPost(url, executeParams);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return  result;
    }

    private static  String getLoginInfo(){
        String access_token = getToken();
        String params = "{\"step_name\":\"login\",\"username\":\"18335109596\",\"nsrsbh\":\"91140000MA0L9DU60L\",\"zrrlx\":\"\",\"password\":\"Sxjt102030\",\"smsCode\":\"123456\",\"dqbm\":\"1400\"}";
        String executeParams = "{'org_code':'600001','sdyh':'18335109596','nsrsbh':'91140000MA0L9DU60L','serve_code':'yspt_sdyh_login','access_token':'" + access_token + "'," + "'data':'" + getAesData(params, "gyZ9qah3QzPrmQZa") + "'}";
        String url = "http://172.16.95.5:38080/yspt/sdpAccept";
        System.out.println(params);
        System.out.println("登录请求地址："+ url);
        System.out.println("登录请求参数："+ executeParams);
        String result = null;
        try {
            result = sendPost(url, executeParams);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("登录返回报文: "+ result);
        return result;
    }

    private static  String getLoginIn(String code){
        String access_token = getToken();
        String params = "{\"step_name\":\"sms_login\",\"username\":\"18335109596\",\"nsrsbh\":\"91140000MA0L9DU60L\",\"zrrlx\":\"03\",\"password\":\"Sxjt102030\",\"smsCode\":\""+code+"\",\"dqbm\":\"1400\"}";
        String executeParams = "{'org_code':'600001','sdyh':'18335109596','nsrsbh':'91140000MA0L9DU60L','serve_code':'yspt_sdyh_login','access_token':'" + access_token + "'," + "'data':'" + getAesData(params, "gyZ9qah3QzPrmQZa") + "'}";
        String url = "http://172.16.95.5:38080/yspt/sdpAccept";
        System.out.println(params);
        System.out.println("登录请求地址："+ url);
        System.out.println("登录请求参数："+ executeParams);
        String result = null;
        try {
            result = sendPost(url, executeParams);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("登录返回报文: "+ result);
        return result;
    }

    private static  String getToken(){
        String url = "http://172.16.95.5:38080/yspt/token";
        String param = ("org_code=600001"  );
        param += ("&serve_code=61328001"  );
        param += ("&auth_code=ell6xzkNssd7Num2"  );
        param += ("&signature=" + genSignature("E4esJCD4ErxDWgFJ"));
        System.out.println("token param: "+ param);
        System.out.println("url:"+url);
        String result = sendPost(url, param);
        System.out.println("result:"+result);
        Map map = (Map) JSON.parse(result);
        String access_token = map.get("access_token").toString();
        System.out.println("access_token:"+access_token);
        return access_token;
    }

    private static String genSignature(String secret) {
        StringBuffer data = new StringBuffer("org_code=");
        data.append("600001")
                .append("&serve_code=").append("61328001")
                .append("&auth_code=").append("ell6xzkNssd7Num2");
        String signature = HMACSHA256(data.toString().getBytes(), secret.getBytes());
        return signature;
    }

    public static String HMACSHA256(byte[] data, byte[] key)
    {
        try  {
            SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(signingKey);
            return byte2hex(mac.doFinal(data));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String byte2hex(byte[] b)
    {
        StringBuilder hs = new StringBuilder();
        String stmp;
        for (int n = 0; b!=null && n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0XFF);
            if (stmp.length() == 1)
                hs.append('0');
            hs.append(stmp);
        }
        return hs.toString().toUpperCase();
    }

    public static String sendPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            //下面注释的setRequestProperty可要可不要
            conn.setRequestProperty("accept", "application/json, text/javascript, */*; q=0.01");
            conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            OutputStreamWriter outWriter = new OutputStreamWriter(conn.getOutputStream(), "utf-8");
            out = new PrintWriter(outWriter);
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送 POST 请求出现异常！"+e);
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally{
            try{
                if(out!=null){
                    out.close();
                }
                if(in!=null){
                    in.close();
                }
            }
            catch(IOException ex){
                ex.printStackTrace();
            }
        }
        return result;
    }

    private static String getAesData(String params, String encryptKey) {
        String data_AesCBC = null;
        try {
            System.out.println(params);
            data_AesCBC = aesEncrypt(params, encryptKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("data_AES:"+ data_AesCBC);
        return data_AesCBC;
    }

    public static String aesEncrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    public static String base64Encode(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    public static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));
        return cipher.doFinal(content.getBytes("utf-8"));
    }
}

